package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.WorkAwardWinners;
import com.ruoyi.system.mapper.WorkAwardMapper;
import com.ruoyi.system.domain.WorkAward;
import com.ruoyi.system.service.IWorkAwardService;

/**
 * 获奖Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-03
 */
@Service
public class WorkAwardServiceImpl implements IWorkAwardService 
{
    @Autowired
    private WorkAwardMapper workAwardMapper;

    /**
     * 查询获奖
     * 
     * @param id 获奖主键
     * @return 获奖
     */
    @Override
    public WorkAward selectWorkAwardById(Long id)
    {
        return workAwardMapper.selectWorkAwardById(id);
    }

    /**
     * 查询获奖列表
     * 
     * @param workAward 获奖
     * @return 获奖
     */
    @Override
    public List<WorkAward> selectWorkAwardList(WorkAward workAward)
    {
        return workAwardMapper.selectWorkAwardList(workAward);
    }

    /**
     * 新增获奖
     * 
     * @param workAward 获奖
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWorkAward(WorkAward workAward)
    {
        int rows = workAwardMapper.insertWorkAward(workAward);
        insertWorkAwardWinners(workAward);
        return rows;
    }

    /**
     * 修改获奖
     * 
     * @param workAward 获奖
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWorkAward(WorkAward workAward)
    {
        workAwardMapper.deleteWorkAwardWinnersByWorkAwardId(workAward.getId());
        insertWorkAwardWinners(workAward);
        return workAwardMapper.updateWorkAward(workAward);
    }

    /**
     * 批量删除获奖
     * 
     * @param ids 需要删除的获奖主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkAwardByIds(Long[] ids)
    {
        workAwardMapper.deleteWorkAwardWinnersByWorkAwardIds(ids);
        return workAwardMapper.deleteWorkAwardByIds(ids);
    }

    /**
     * 删除获奖信息
     * 
     * @param id 获奖主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWorkAwardById(Long id)
    {
        workAwardMapper.deleteWorkAwardWinnersByWorkAwardId(id);
        return workAwardMapper.deleteWorkAwardById(id);
    }

    /**
     * 新增获奖完成人信息
     * 
     * @param workAward 获奖对象
     */
    public void insertWorkAwardWinners(WorkAward workAward)
    {
        List<WorkAwardWinners> workAwardWinnersList = workAward.getWorkAwardWinnersList();
        Long id = workAward.getId();
        if (StringUtils.isNotNull(workAwardWinnersList))
        {
            List<WorkAwardWinners> list = new ArrayList<WorkAwardWinners>();
            for (WorkAwardWinners workAwardWinners : workAwardWinnersList)
            {
                workAwardWinners.setWorkAwardId(id);
                list.add(workAwardWinners);
            }
            if (list.size() > 0)
            {
                workAwardMapper.batchWorkAwardWinners(list);
            }
        }
    }
}
